package com.ruoyi.supermarket.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.supermarket.bean.UserCollectionRes;
import com.ruoyi.supermarket.domain.Product;
import com.ruoyi.supermarket.domain.UserCollection;
import com.ruoyi.supermarket.domain.UserInfo;
import com.ruoyi.supermarket.service.ProductService;
import com.ruoyi.supermarket.service.UserCollectionService;
import com.ruoyi.supermarket.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户收藏Controller
 * 
 * @author 李其伟
 * @date 2023-08-14
 */
@RestController
@RequestMapping("/supermarket/userCollection")
public class UserCollectionController extends BaseController
{
    @Autowired
    private UserCollectionService userCollectionService;

    @Autowired
    private ProductService productService;

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 查询用户收藏列表
     */
    @PreAuthorize("@ss.hasPermi('supermarket:userCollection:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserCollection userCollection)
    {
        startPage();
        List<UserCollection> list = userCollectionService.selectUserCollectionList(userCollection);
        TableDataInfo tableDataInfo =  getDataTable(list);

        List<UserCollection> userCollectionList = (List<UserCollection>) tableDataInfo.getRows();
        if(userCollectionList.size()==0){
            return tableDataInfo;
        }

        String[] skus = userCollectionList.stream().map(UserCollection::getSku).toArray(String[]::new);
        List<Product> productList = productService.selectProductBySkus(skus);
        Map<String, Product> productMap = productList.stream().collect(Collectors.toMap(Product::getSku, product->product));

        Long[] ids = userCollectionList.stream().map(UserCollection::getUserId).toArray(Long[]::new);
        List<UserInfo> userInfoList = userInfoService.selectUserInfoByIds(ids);
        Map<Long, UserInfo> userInfoMap = userInfoList.stream().collect(Collectors.toMap(UserInfo::getId, userInfo->userInfo));

        List<UserCollectionRes> userCollectionResList = new ArrayList<>();
        for(UserCollection uc : userCollectionList){
            UserCollectionRes ucr = new UserCollectionRes();
            ucr.setUserCollection(uc);
            ucr.setProduct(productMap.get(uc.getSku()));
            ucr.setUserInfo(userInfoMap.get(uc.getUserId()));
            userCollectionResList.add(ucr);
        }
        tableDataInfo.setRows(userCollectionResList);
        return tableDataInfo;
    }

    /**
     * 导出用户收藏列表
     */
    @PreAuthorize("@ss.hasPermi('supermarket:userCollection:export')")
    @Log(title = "用户收藏", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserCollection userCollection)
    {
        List<UserCollection> list = userCollectionService.selectUserCollectionList(userCollection);
        ExcelUtil<UserCollection> util = new ExcelUtil<UserCollection>(UserCollection.class);
        util.exportExcel(response, list, "用户收藏数据");
    }

    /**
     * 获取用户收藏详细信息
     */
    @PreAuthorize("@ss.hasPermi('supermarket:userCollection:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userCollectionService.selectUserCollectionById(id));
    }

    /**
     * 新增用户收藏
     */
    @PreAuthorize("@ss.hasPermi('supermarket:userCollection:add')")
    @Log(title = "用户收藏", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserCollection userCollection)
    {
        return toAjax(userCollectionService.insertUserCollection(userCollection));
    }

    /**
     * 修改用户收藏
     */
    @PreAuthorize("@ss.hasPermi('supermarket:userCollection:edit')")
    @Log(title = "用户收藏", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserCollection userCollection)
    {
        return toAjax(userCollectionService.updateUserCollection(userCollection));
    }

    /**
     * 删除用户收藏
     */
    @PreAuthorize("@ss.hasPermi('supermarket:userCollection:remove')")
    @Log(title = "用户收藏", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userCollectionService.deleteUserCollectionByIds(ids));
    }
}
